<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveRecord::Acts::Price::ClassMethods</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveRecord::Acts::Price::ClassMethods</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/lib/acts_as_price_rb.html">
                lib/acts_as_price.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000002">acts_as_price</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000002" class="method-detail">
        <a name="M000002"></a>

        <div class="method-heading">
          <a href="#M000002" class="method-signature">
          <span class="method-name">acts_as_price</span><span class="method-args">(column_name, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Specify the column_name that needs to act as price
</p>
<p>
Valid options:
</p>
<ul>
<li>:validates =&gt; true (performs validates_presence_of and
validates_numericality_of)

</li>
<li>:comma_seperated =&gt; true (set and get the price as a comma-seperated
value)

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000002-source">
<pre>
    <span class="ruby-comment cmt"># File lib/acts_as_price.rb, line 15</span>
15:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">acts_as_price</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">options</span> = {}
16:           <span class="ruby-identifier">validates</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">:presence</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>, <span class="ruby-identifier">:numericality</span> =<span class="ruby-operator">&gt;</span> {<span class="ruby-identifier">:greater_than</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>} <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:validates</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span>
17:           <span class="ruby-identifier">currency</span>, <span class="ruby-identifier">comma</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:currency</span>], <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:comma_seperated</span>]
18:           
19:           <span class="ruby-comment cmt">#setters</span>
20:           <span class="ruby-identifier">define_method</span>(<span class="ruby-node">&quot;#{column_name}=&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
21:             <span class="ruby-keyword kw">super</span>((<span class="ruby-identifier">val</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;,&quot;</span>, <span class="ruby-value str">&quot;.&quot;</span>).<span class="ruby-identifier">to_f</span> <span class="ruby-operator">*</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>)
22:           <span class="ruby-keyword kw">end</span>
23:           <span class="ruby-identifier">alias_method</span>(<span class="ruby-value str">&quot;price=&quot;</span>, <span class="ruby-node">&quot;#{column_name}=&quot;</span>)
24:           
25:           <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;price_in_cents=&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
26:             <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value str">&quot;price=&quot;</span>, <span class="ruby-identifier">val</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>)
27:           <span class="ruby-keyword kw">end</span>
28:           <span class="ruby-identifier">alias_method</span> <span class="ruby-node">&quot;#{column_name}_in_cents=&quot;</span>, <span class="ruby-value str">&quot;price_in_cents=&quot;</span>
29:           
30:           <span class="ruby-comment cmt">#getters</span>
31:           <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">column_name</span>) <span class="ruby-keyword kw">do</span>
32:             <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">super</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span><span class="ruby-value">.0</span>
33:               <span class="ruby-value str">&quot;&quot;</span>
34:             <span class="ruby-keyword kw">else</span>
35:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">comma</span>
36:                 <span class="ruby-identifier">return_val</span> = <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">&quot;%.2f&quot;</span>, <span class="ruby-keyword kw">super</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;.&quot;</span>, <span class="ruby-value str">&quot;,&quot;</span>)
37:               <span class="ruby-keyword kw">else</span>
38:                 <span class="ruby-identifier">return_val</span> = <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">&quot;%.2f&quot;</span>, <span class="ruby-keyword kw">super</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>)
39:               <span class="ruby-keyword kw">end</span>
40:               <span class="ruby-identifier">return_val</span> = <span class="ruby-identifier">currency</span> <span class="ruby-value">? </span><span class="ruby-node">&quot;#{currency}. #{return_val}&quot;</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">return_val</span>
41:               <span class="ruby-identifier">return_val</span>
42:             <span class="ruby-keyword kw">end</span>
43:           <span class="ruby-keyword kw">end</span>
44:           <span class="ruby-identifier">alias_method</span> <span class="ruby-value str">&quot;price&quot;</span>, <span class="ruby-identifier">column_name</span>
45:           
46:           <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;price_in_cents&quot;</span>) <span class="ruby-keyword kw">do</span>
47:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
48:               ((<span class="ruby-identifier">send</span> <span class="ruby-identifier">column_name</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-node">&quot;#{currency}. &quot;</span>, <span class="ruby-value str">&quot;&quot;</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;,&quot;</span>, <span class="ruby-value str">&quot;.&quot;</span>).<span class="ruby-identifier">to_f</span> <span class="ruby-operator">*</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">to_i</span>
49:             <span class="ruby-keyword kw">else</span>
50:               ((<span class="ruby-identifier">send</span> <span class="ruby-identifier">column_name</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;,&quot;</span>, <span class="ruby-value str">&quot;.&quot;</span>).<span class="ruby-identifier">to_f</span> <span class="ruby-operator">*</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">to_i</span>
51:             <span class="ruby-keyword kw">end</span>
52:           <span class="ruby-keyword kw">end</span>
53:           <span class="ruby-identifier">alias_method</span> <span class="ruby-node">&quot;#{column_name}_in_cents&quot;</span>, <span class="ruby-value str">&quot;price_in_cents&quot;</span>
54:           
55:           <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;currency&quot;</span>) <span class="ruby-keyword kw">do</span>
56:             <span class="ruby-identifier">currency</span>
57:           <span class="ruby-keyword kw">end</span>
58:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>